package net.stratusprogramming.school.Game.Framework.World;

import org.newdawn.slick.tiled.TiledMap;

import net.stratusprogramming.school.Game.Framework.Scene.GameObject;
import net.stratusprogramming.school.Game.Framework.Scene.Group;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.System.Logger;
import net.stratusprogramming.school.Game.Framework.System.SaveMgr;

public class GameObjectMgr extends Group {
	
	private TiledMap map;
	
	public GameObjectMgr() {

	}
	

	@Override
	public void load(SaveMgr saveMgr) {
		children.clear();
		map = InstanceMgr.map.getMap();
		for(int g = 0; g < map.getObjectGroupCount(); g++) {
			for(int o = 0; o < map.getObjectCount(g); o++) {
				if(map.getObjectType(g, o).equalsIgnoreCase("object")) {
					String packageStr = GameObjectMgr.class.getPackage().getName();
					String className = "net.stratusprogramming.school.Game.GameObjects."+map.getObjectProperty(g, o, "class", "");
					try {
						// create game object
						Class<? extends GameObject> gameObject = Class.forName(className).asSubclass(GameObject.class);
						GameObject go = gameObject.newInstance(); 
						
						// set position/dimention
						go.setHeight(map.getObjectHeight(g, o));
						go.setWidth(map.getObjectWidth(g, o));
						go.setX(map.getObjectX(g, o));
						go.setY(map.getObjectY(g, o));
						
						// add object
						add(go);
						Logger.logln("GameObjectMgr", "\""+className+"\" Loaded!");
					
					} catch (ClassNotFoundException e) {
						Logger.logln("GameObjectMgr", "Failed to find class \""+className+"\"");
						e.printStackTrace();
					} catch (InstantiationException e) {
						Logger.logln("GameObjectMgr", "Failed to load class \""+className+"\"");
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						Logger.logln("GameObjectMgr", "Failed to load class \""+className+"\"");
						e.printStackTrace();
					}
				}
			}
        }
		super.load(saveMgr);
	}	
}
